package cn.dnui_cjy529.dnuifood_16110100529.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_cjy529.dnuifood_16110100529.R;
import cn.dnui_cjy529.dnuifood_16110100529.bean.ShopBean;
import cn.dnui_cjy529.dnuifood_16110100529.controller.FoodListActivity;
import cn.dnui_cjy529.dnuifood_16110100529.model.Constant;
import cn.dnui_cjy529.dnuifood_16110100529.model.UserModel;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private Context mContext;
    private List<ShopBean> mDataList;
    private LayoutInflater mLayoutInflater;
    private String id;
    //private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public ShopAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        // v.setOnClickListener((View.OnClickListener) this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ShopBean entity = (ShopBean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());
        //  viewHolder.image.setImageResource(Integer.parseInt(entity.getPic()));

        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(viewHolder.image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到具体店铺界面
                String str = String.valueOf(mDataList.get(position).getShop_id());
                String str1 = String.valueOf(mDataList.get(position).getShopname());
                String str2 = String.valueOf(mDataList.get(position).getUser_id());



                Intent intent = new Intent(mContext,FoodListActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("shop_id",str);
                intent.putExtra("shopname",str1);
                intent.putExtra("user_id",str2);
                mContext.startActivity(intent);

                // mContext.startActivity(intent);

                // Toast.makeText(mContext,str,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDataList==null)
            return 0;
        else
            return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        RatingBar res_bar;
        TextView res_name,res_address;
        ImageView image;
        public ViewHolder(View view) {
            super(view);
            res_name=(TextView) view.findViewById(R.id.res_name);
            res_address=(TextView) view.findViewById(R.id.res_address);
            image=(ImageView)view.findViewById(R.id.res_image);
            res_bar=(RatingBar) view.findViewById(R.id.ratingBar1);
        }
    }




//@Override
//    public void onClick(View v){
//        if (mOnItemClickListener != null){
//            mOnItemClickListener.onItemClick(v,v.getTag());
//        }
//}

}
